package com.campus.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.api.util.UserContext;
import com.campus.common.result.Result;
import com.campus.common.student.job.entity.Student;
import com.campus.common.student.mapper.StudentMapper;
import com.campus.common.teacher.mapper.TeacherMapper;
import com.campus.common.user.entity.pojo.User;
import com.campus.common.user.mapper.AuthMapper;
import com.campus.course.dto.entity.CourseRecords;
import com.campus.course.dto.entity.CourseSchedule;
import com.campus.course.dto.entity.Courses;
import com.campus.course.dto.form.ArrangeCourseForm;
import com.campus.course.dto.vo.CourseTable;
import com.campus.course.mapper.CourseRecordsMapper;
import com.campus.course.mapper.CourseScheduleMapper;
import com.campus.course.mapper.CoursesMapper;
import com.campus.course.service.ICourseScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程时间表 服务实现类
 * </p>
 *
 * @author maojr
 * @since 2024-11-16
 */
@Service
public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleMapper, CourseSchedule> implements ICourseScheduleService {


    @Autowired
    private CourseScheduleMapper courseScheduleMapper;

    @Autowired
    private CoursesMapper coursesMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CourseRecordsMapper courseRecordsMapper;

    @Autowired
    private AuthMapper authMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Result arrangeCourse(ArrangeCourseForm arrangeCourseForm) {
        //查询该时间段教室是否被占用
        QueryWrapper<CourseSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("week_day", arrangeCourseForm.getWeekDay())
                .eq("day",arrangeCourseForm.getDay())
                .eq("time",arrangeCourseForm.getTime())
                .eq("address",arrangeCourseForm.getAddress());

        if (courseScheduleMapper.selectOne(scheduleQueryWrapper) != null) {
            return Result.fail("排课教室冲突，此教室已被占用",null);
        }

        //查询教师已教课程是否有时间上冲突
        List<Courses> coursesList = coursesMapper.selectList(
                 new QueryWrapper<Courses>().eq("teacher_id",arrangeCourseForm.getTeacherId())
        );
        //遍历每种课的每节时间，是否冲突
        for (Courses course : coursesList) {
            QueryWrapper<CourseSchedule> scheduleQueryWrapper1 = new QueryWrapper<>();
            scheduleQueryWrapper1.eq("course_id",course.getId());
            List<CourseSchedule> schedules = courseScheduleMapper.selectList(scheduleQueryWrapper1);
            for (CourseSchedule schedule : schedules) {
                if (arrangeCourseForm.getWeekDay() == schedule.getWeekDay() &&
                    arrangeCourseForm.getDay() == schedule.getDay() &&
                        arrangeCourseForm.getTime() == schedule.getTime() ) {
                    return Result.fail("该教师已有课程安排，无法排课",null);
                }
            }
        }
        //加入记录
        CourseSchedule courseSchedule = new CourseSchedule();
        BeanUtils.copyProperties(arrangeCourseForm,courseSchedule);
        courseScheduleMapper.insert(courseSchedule);
        return Result.success("排课成功,记录已加入数据库",null);
    }

    @Override
    public Result getStudentCourseTable(int weekDay) {
        int userId = UserContext.getUserId();
        //获取学生学号(id)
        Student student = studentMapper.selectOne(
                new QueryWrapper<Student>().eq("user_id", userId)
        );
        //获取学生的所选课程
        List<CourseRecords> records = courseRecordsMapper.selectList(
                new QueryWrapper<CourseRecords>().eq("student_id",student.getId())
        );
        CourseTable[] tables = new CourseTable[5];
        for (int i = 0; i < tables.length; i++ ) {
            tables[i] = new CourseTable();
        }
        for (CourseRecords record : records) {
            List<CourseSchedule> schedules = courseScheduleMapper.selectList(
                    new QueryWrapper<CourseSchedule>().eq("course_id",record.getCourseId())
                            .eq("week_day",weekDay)
            );
            for (CourseSchedule schedule : schedules) {
                //判断该节课是在哪一天哪个时间段，然后加入到对应的课表对象
                Courses course = coursesMapper.selectById(schedule.getCourseId());
                User user = authMapper.selectById(userId);
                String TeacherName = authMapper.selectById(teacherMapper.selectById(course.getTeacherId()).getUserId()).getName();
                switch (schedule.getDay()) {
                    case 1:
                        tables[schedule.getTime() - 1].setMonday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                    case 2:
                        tables[schedule.getTime() - 1].setTuesday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                    case 3:
                        tables[schedule.getTime() - 1].setWednesday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                    case 4:
                        tables[schedule.getTime() - 1].setThursday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                    case 5:
                        tables[schedule.getTime() - 1].setFriday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                    case 6:
                        tables[schedule.getTime() - 1].setSaturday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                    case 7:
                        tables[schedule.getTime() - 1].setSunday(schedule.getAddress()+"Y#"+course.getCourseName()+"@"+TeacherName);break;
                }
            }
        }

        return Result.success(tables,null);
    }

    @Override
    public Result deleteCourseSchedule(int scheduleId) {

        if (courseScheduleMapper.deleteById(scheduleId) > 0) {
            return Result.success("取消排课成功");
        }
        else {
            return Result.fail("取消排课失败");
        }
    }

}
